package pharmacy

class DrugCategoryController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [drugCategoryInstanceList: DrugCategory.list(params), drugCategoryInstanceTotal: DrugCategory.count()]
    }

    def create = {
        def drugCategoryInstance = new DrugCategory()
        drugCategoryInstance.properties = params
        return [drugCategoryInstance: drugCategoryInstance]
    }

    def save = {
        def drugCategoryInstance = new DrugCategory(params)
        if (drugCategoryInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), drugCategoryInstance.id])}"
            redirect(action: "show", id: drugCategoryInstance.id)
        }
        else {
            render(view: "create", model: [drugCategoryInstance: drugCategoryInstance])
        }
    }

    def show = {
        def drugCategoryInstance = DrugCategory.get(params.id)
        if (!drugCategoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
            redirect(action: "list")
        }
        else {
            [drugCategoryInstance: drugCategoryInstance]
        }
    }

    def edit = {
        def drugCategoryInstance = DrugCategory.get(params.id)
        if (!drugCategoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [drugCategoryInstance: drugCategoryInstance]
        }
    }

    def update = {
        def drugCategoryInstance = DrugCategory.get(params.id)
        if (drugCategoryInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (drugCategoryInstance.version > version) {
                    
                    drugCategoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'drugCategory.label', default: 'DrugCategory')] as Object[], "Another user has updated this DrugCategory while you were editing")
                    render(view: "edit", model: [drugCategoryInstance: drugCategoryInstance])
                    return
                }
            }
            drugCategoryInstance.properties = params
            if (!drugCategoryInstance.hasErrors() && drugCategoryInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), drugCategoryInstance.id])}"
                redirect(action: "show", id: drugCategoryInstance.id)
            }
            else {
                render(view: "edit", model: [drugCategoryInstance: drugCategoryInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def drugCategoryInstance = DrugCategory.get(params.id)
        if (drugCategoryInstance) {
            try {
                drugCategoryInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'drugCategory.label', default: 'DrugCategory'), params.id])}"
            redirect(action: "list")
        }
    }
}
